package org.aaron.mp3player.xml;

import java.util.List;

import org.aaron.mp3player.model.Mp3Info;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class Mp3ListContentHandler extends DefaultHandler {
	public static final String RESOURSE_TAG = "resource";
	private List<Mp3Info> mp3Infos;
	private Mp3Info mp3Info;
	private String tagname;

	public Mp3ListContentHandler(List<Mp3Info> mp3Infos) {
		this.mp3Infos=mp3Infos;
	}
	@Override
	public void startDocument() throws SAXException {
		super.startDocument();
	}

	@Override
	public void endDocument() throws SAXException {
		super.endDocument();
	}

	@Override
	public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
		tagname = localName;
		if (RESOURSE_TAG.equals(tagname)) {
			mp3Info = new Mp3Info();
		}

	}

	@Override
	public void endElement(String uri, String localName, String qName) throws SAXException {
		if (RESOURSE_TAG.equals(qName)) {
			mp3Infos.add(mp3Info);
		}
		tagname="";
	}

	@Override
	public void characters(char[] ch, int start, int length) throws SAXException {
		String tempValue = new String(ch, start, length);
		if ("id".equals(tagname)) {
			mp3Info.setId(tempValue);
		}
		if ("mp3.name".equals(tagname)) {
			mp3Info.setMp3Name(tempValue);
		}
		if ("mp3.size".equals(tagname)) {
			mp3Info.setMp3Size(tempValue);
		}
		if ("lrc.name".equals(tagname)) {
			mp3Info.setLrcName(tempValue);
		}
		if ("lrc.size".equals(tagname)) {
			mp3Info.setLrcSize(tempValue);
		}
	}
}
